package com.knowif.backend.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.knowif.util.WechatMiniUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.knowif.pojo.UserWechat;
import com.knowif.service.UserWechat_service;

@Service
public class UserWeChatProxy {
    @Autowired
    private UserWechat_service userWechat_service;
    @Autowired
    private WechatMiniUtil wechatMiniUtil;

    public void weChatproxy(HttpServletRequest request, HttpServletResponse response){
        System.out.println("新的controller======================================");
        try {
            BufferedReader bufferedReader = request.getReader();
            String temp = "";
            StringBuilder res = new StringBuilder();
            while ( (temp = bufferedReader.readLine()) != null){
                res.append(temp);
            }
            System.out.println(res.toString());
            Map<String,String> map = new HashMap<String,String>();
            Document doc = null;
            doc = DocumentHelper.parseText(res.toString());
            Element root = doc.getRootElement();
            @SuppressWarnings("unchecked")
			List<Element> list = (List<Element>)root.elements();
            for(Element e:list){
                map.put(e.getName(), e.getText());
            }
            if(map.get("MsgType").equals("event")){
                if(map.get("Event").equals("subscribe")){
                    String openid = map.get("FromUserName");//openid 查出unionId 入库，
                    JSONObject obj = wechatMiniUtil.getWeChatUserInfo(openid);
                    if(obj != null){
                        String unionid = obj.getString("unionid");
                        if(unionid == null || unionid.isEmpty()){
                            return;
                        }else{
                            UserWechat userWechat = userWechat_service.selectByUnionId(unionid);
                            if(userWechat == null){
                                userWechat = new UserWechat();
                                userWechat.setUnionid(unionid);
                                userWechat.setPlatform(openid);
                                userWechat_service.insertSelective(userWechat);
                            }else{
                                if(userWechat.getPlatform() == null || userWechat.getPlatform().isEmpty()){
                                    userWechat.setPlatform(openid);
                                    userWechat_service.updateByPrimaryKeySelective(userWechat);
                                }
                            }
                        }

                    }

                }
            }
            System.out.println("openid ===================>" + map.get("FromUserName"));
            System.out.println("MsgType ==================>" + map.get("MsgType"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }


}
